﻿package mortal.game.scene3D.model.player
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import frEngine.core.*;
    import frEngine.render.layer.*;
    import mortal.game.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.util.*;

    public class WizardSmallPlayer extends ActionPlayer
    {
        protected var _bodySprite:UserPlayer;
        protected var _maxRadius:int = 60;
        protected var _cycleRadiusW:int = 30;
        protected var _cycleRadiusH:int = 15;
        protected var _frameSpeed:Number = 3;
        protected var _targetPointX:Number = 0;
        protected var _targetPointY:Number = 0;
        protected var _checkDistance:Number = 10;
        protected var _currentStatus:int = 1;
        private var _waitCount:int = 0;
        private var _cycleCount:int = 0;
        private var _cycleAngle:int = 0;
        private var _cycleSpeed:int = 2;
        private var _insertTime:int;
        private var _insertAngle:Number;
        protected var _direction:Number = 0;
        protected var _targetDirection:Number;
        private var _isFrameUpdateNamePosition:Boolean = false;

        public function WizardSmallPlayer(param1:int = 2)
        {
            super(param1);
            return;
        }// end function

        protected function onTargetPointChange(param1:AstarTurnPoint) : void
        {
            if (!this._bodySprite)
            {
                return;
            }
            this.updateTargetXY(param1);
            return;
        }// end function

        private function updateTargetXY(param1:AstarTurnPoint) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            return;
        }// end function

        public function addToUser(param1:UserPlayer, param2:int) : void
        {
            var _loc_4:TModel = null;
            var _loc_5:String = null;
            var _loc_6:String = null;
            if (!this._bodySprite && param1 != null)
            {
                this._bodySprite = param1;
                if (ThingUtil.entityUtil.isWizardShow && !GameMapUtil.isInHideSpriteMap && this._bodySprite.parent)
                {
                    this.addToStage();
                }
                this.x2d = param1.x2d + this._cycleRadiusW;
                this.y2d = param1.y2d;
                this.y = 10;
            }
            var _loc_3:* = WizardConfig.instance.getSpriteByCodeAndLevel(param2, 1);
            if (_loc_3)
            {
                _loc_4 = ModelConfig.instance.getInfoByCode(int(_loc_3.smallModelId));
                if (_loc_4)
                {
                    _loc_5 = _loc_4.mesh1 + ".md5mesh";
                    _loc_6 = _loc_4.bone1 + ".skeleton";
                    load(_loc_5, _loc_6, _loc_4.texture1, null, 0, EBlendType.BLEND_SCREEN);
                    this.setLayer(Layer3DManager.AlphaLayer0);
                    if (this._bodySprite && this._bodySprite is RolePlayer)
                    {
                        RoleResourceUtil.changeRoleModel(EEntityAttribute._EAttributeSprite, [_loc_5, _loc_6, _loc_4.texture1]);
                    }
                }
            }
            this.changeAction(ActionName.Walking);
            return;
        }// end function

        public function removeToUser() : void
        {
            this.dispose();
            return;
        }// end function

        public function addToStage() : void
        {
            Game.scene.playerLayer.addChild(this);
            return;
        }// end function

        public function removeFromStage() : void
        {
            if (this.parent)
            {
                Game.scene.playerLayer.removeChild(this);
            }
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose(param1);
            if (this._bodySprite)
            {
                this._bodySprite.pointChangeHandler = null;
            }
            this._bodySprite = null;
            return;
        }// end function

        override public function update(param1:int = 0) : void
        {
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            super.update(param1);
            if (!this._bodySprite)
            {
                return;
            }
            var _loc_2:* = MathUitl.getDistance(this.x2d, this._bodySprite.x2d, this.y2d, this._bodySprite.y2d);
            if (_loc_2 > 600)
            {
                this.x2d = this._bodySprite.x2d + this._cycleRadiusW;
                this.y2d = this._bodySprite.y2d;
                this.direction = 90;
                return;
            }
            if (_loc_2 > 40)
            {
                this._frameSpeed = _loc_2 / 20;
                _loc_3 = MathUitl.getRadians(this._bodySprite.direction + 180);
                this._targetPointX = this._bodySprite.x2d + this._cycleRadiusW * Math.cos(_loc_3);
                this._targetPointY = this._bodySprite.y2d + this._cycleRadiusH * Math.sin(_loc_3);
                this._currentStatus = 0;
                this._cycleCount = 0;
            }
            if (this._currentStatus == 0)
            {
                _loc_4 = MathUitl.getDistance(this.x2d, this._targetPointX, this.y2d, this._targetPointY);
                _loc_5 = 0;
                _loc_6 = 0;
                if (_loc_4 < this._frameSpeed)
                {
                    this.x2d = this._targetPointX;
                    this.y2d = this._targetPointY;
                    this._currentStatus = 1;
                    this._waitCount = 0;
                    return;
                }
                _loc_8 = Math.atan2(this._targetPointY - y2d, this._targetPointX - x2d);
                _loc_5 = Math.cos(_loc_8) * this._frameSpeed;
                _loc_6 = Math.sin(_loc_8) * this._frameSpeed;
                x2d = x2d + _loc_5;
                y2d = y2d + _loc_6;
                _loc_7 = MathUitl.getAngle(Math.atan2(_loc_6, _loc_5));
                this.direction = _loc_7;
            }
            if (this._insertTime > 0)
            {
                if (this._insertTime > 1)
                {
                    this.updateDirection(this._direction + this._insertAngle);
                    var _loc_9:String = this;
                    var _loc_10:* = this._insertTime - 1;
                    _loc_9._insertTime = _loc_10;
                }
                else
                {
                    this.updateDirection(this._targetDirection);
                    this._insertTime = 0;
                }
            }
            return;
        }// end function

        private function cycleMove() : void
        {
            if (this._cycleAngle >= 0 && this._cycleAngle < 30)
            {
                this._currentStatus = 3;
                return;
            }
            var _loc_1:* = this._cycleAngle * Math.PI / 180;
            x2d = this._bodySprite.x2d + this._cycleRadiusW * Math.cos(_loc_1);
            y2d = this._bodySprite.y2d + this._cycleRadiusH * Math.sin(_loc_1);
            this._cycleAngle = this._cycleAngle + this._cycleSpeed;
            if (this._cycleAngle > 360)
            {
                this._cycleAngle = 0;
                var _loc_2:String = this;
                var _loc_3:* = this._cycleCount + 1;
                _loc_2._cycleCount = _loc_3;
            }
            return;
        }// end function

        public function updateDirection(param1:Number) : void
        {
            if (param1 > 180)
            {
                param1 = param1 - 360;
            }
            else if (param1 < -180)
            {
                param1 = param1 + 360;
            }
            this._direction = param1;
            this.updateBodyDirection();
            return;
        }// end function

        protected function updateBodyDirection() : void
        {
            super.direction = this._direction;
            return;
        }// end function

        override public function set direction(param1:Number) : void
        {
            var _loc_2:Number = NaN;
            if (this._targetDirection == param1)
            {
                return;
            }
            this._targetDirection = param1;
            _loc_2 = this._targetDirection - this._direction;
            if (_loc_2 > 180)
            {
                _loc_2 = _loc_2 - 360;
            }
            else if (_loc_2 < -180)
            {
                _loc_2 = _loc_2 + 360;
            }
            this._insertTime = Math.abs(Math.floor(_loc_2 / 12));
            this._insertTime = this._insertTime < 6 ? (6) : (this._insertTime);
            this._insertTime = this._insertTime > 12 ? (12) : (this._insertTime);
            this._insertAngle = _loc_2 / this._insertTime;
            if (this._insertTime == 0)
            {
                this.updateDirection(this._targetDirection);
            }
            return;
        }// end function

        override public function get direction() : Number
        {
            return this._direction;
        }// end function

    }
}
